package com.yegs.jltqc.serviceImpl;

import com.yegs.jltqc.TO.NamePageQuery;
import com.yegs.jltqc.TO.ProjectPageQuery;
import com.yegs.jltqc.VO.BaseUser;
import com.yegs.jltqc.VO.Project;
import com.yegs.jltqc.common.StoreException;
import com.yegs.jltqc.dao.ProjectRepository;
import com.yegs.jltqc.service.ProjectService;;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectRepository projectRepository;

    @Override
    public Project save(Project project) {
        if(project.getCinvcode() == null){ throw new StoreException("产品信息不可为空"); }
        if(project.getVersion() == null){ throw new StoreException("方案版本不可为空"); }
        if(project.getId() == null && projectRepository.findProjectByCinvcodeAndVersion(project.getCinvcode(), project.getVersion()) != null){
            throw new StoreException("已存在同版本方案");
        }
        project = projectRepository.save(project);
        return project;
    }

    @Override
    public List<Project> list() {
        return projectRepository.findAll();
    }

    @Override
    public Project get(Long id) {
        return projectRepository.getOne(id);
    }

    @Override
    public Long getNext(Long id) {
        if(id == null){
            Project project = projectRepository.findFirstByOrderByIdDesc();
            if(project != null) {
                return project.getId();
            }
        }
        Project project = projectRepository.findFirstByIdIsAfter(id);
        if(project == null){ project = projectRepository.findFirstByOrderByIdDesc(); }
        if(project != null){
            return project.getId();
        }
        return null;
    }

    @Override
    public Long getLast(Long id) {
        if(id == null){
            Project project = projectRepository.findFirstByOrderByIdDesc();
            if(project != null) {
                return project.getId();
            }
        }
        else{
            Project project = projectRepository.findFirstByIdBeforeOrderByIdDesc(id);
            if(project == null){ project = projectRepository.findFirstByOrderById(); }
            if(project != null){
                return project.getId();
            }
        }
        return null;
    }

    @Override
    public void del(Long id) {
        projectRepository.deleteById(id);
    }

    @Override
    public Page<Project> query(ProjectPageQuery query) {
        PageRequest request = PageRequest.of(query.getPageIndex() - 1, query.getPageSize());
        if(query.getName() == null || query.getName().trim() == ""){
            if(query.getCinvcode() == null || query.getCinvcode().trim() == ""){
                return projectRepository.findAll(request);
            }
            return projectRepository.findProjectByCinvcodeLike('%' + query.getCinvcode() + '%',request);
        }
        if(query.getCinvcode() == null || query.getCinvcode().trim() == ""){
            return projectRepository.findProjectsByNameLike('%' + query.getName() + '%', request);
        }
        return projectRepository.findProjectByCinvcodeLikeAndNameLike('%' + query.getCinvcode() + '%', '%' + query.getName() + '%', request);
    }

    @Override
    public List<Project> getByCInvCode(String cInvCode) {
        return projectRepository.findProjectByCinvcode(cInvCode);
    }

}


